import socket, threading, queue
import time, random

q = queue.Queue()
class MyTCP_Data_Sender(threading.Thread):

    def __init__(self, id, num):
        super(MyTCP_Data_Sender, self).__init__()
        self.Sensor_id = id
        self.T = num

    def run(self):
        while self.T > 0:
            Data = "DATA:" + "," + self.Sensor_id
            print(self.Sensor_id)
            now = int(time.time())
            timeStruct = time.localtime(now)
            Current_date = time.strftime("%Y-%m-%d", timeStruct)
            Current_time = time.strftime("%H%M%S", timeStruct)
            print("current_date:", Current_date, Current_time)
            Number_flashes = random.choice(
                [0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 2, 0, 0,
                 0, 0, 3])
            if Number_flashes != 0:
                print("2s 闪电数量", Number_flashes)
                Warning_indicator = str(random.randint(1, 3))
                print("告警状态", Warning_indicator)
                Warning_flags_integer = random.choice(["01", "02", "04", "08", "16", "32"])
                print("报警种类", Warning_flags_integer)
                Self_test_flags = '00000'
                if Number_flashes >= 1:
                    now = int(time.time())
                    timeStruct = time.localtime(now)
                    flash_1_date = time.strftime("%Y-%m-%d", timeStruct)
                    flash_1_time = time.strftime("%H%M%S", timeStruct)
                    flash_1_serial_flash = '000'
                    flash_1_distance = '00000'
                    flash_1_angle = '000'
                if Number_flashes >= 2:
                    # time.sleep(2)
                    now = int(time.time())
                    timeStruct = time.localtime(now)
                    flash_2_date = time.strftime("%Y-%m-%d", timeStruct)
                    flash_2_time = time.strftime("%H%M%S", timeStruct)
                    flash_2_serial_flash = '000'
                    flash_2_distance = '00000'
                    flash_2_angle = '000'
                if Number_flashes == 3:
                    # time.sleep(3)
                    now = int(time.time())
                    timeStruct = time.localtime(now)
                    flash_3_date = time.strftime("%Y-%m-%d", timeStruct)
                    flash_3_time = time.strftime("%H%M%S", timeStruct)
                    flash_3_serial_flash = '000'
                    flash_3_distance = '00000'
                    flash_3_angle = '000'
            else:
                Warning_indicator = "0"
                Warning_flags_integer = "00"
                Self_test_flags = '00000'
                flash_1_date = '000000'
                flash_1_time = '000000'
                flash_1_serial_flash = '000'
                flash_1_distance = '00000'
                flash_1_angle = '000'

                flash_2_date = '000000'
                flash_2_time = '000000'
                flash_2_serial_flash = '000'
                flash_2_distance = '00000'
                flash_2_angle = '000'

                flash_3_date = '000000'
                flash_3_time = '000000'
                flash_3_serial_flash = '000'
                flash_3_distance = '00000'
                flash_3_angle = '000'

            Data = Data + "," + Current_date + "," + Current_time + "," + str(Number_flashes) + "," + str(Warning_indicator) + "," + str(Warning_flags_integer) + "," + str(
                Self_test_flags) + "," + flash_1_date + "," + flash_1_time + "," + flash_1_serial_flash + "," + flash_1_distance + "," + flash_1_angle + "," + flash_2_date + "," + flash_2_time + "," + flash_2_serial_flash + "," + flash_2_distance + "," + flash_2_angle + "," + flash_3_date + "," + flash_3_time + "," + flash_3_serial_flash + "," + flash_3_distance + "," + flash_3_angle

            #print(Data)
            q.put(Data)
            self.T = self.T - 1
            time.sleep(2)
            Data = ""


if __name__ == '__main__':
    #启动线程
    client = socket.socket()
    client.connect(('39.106.159.161', 4000)) #连接到阿里云服务器
    # client.connect(('127.0.0.1', 5000))
    for i in range(1,8):
        t = MyTCP_Data_Sender('1902YJ00{}'.format(i), 60)

        t.start()
    try:
        while True:
            if not q.empty():
                data = q.get()
                client.send(data.encode('utf-8'))
                s = client.recv(10).decode()
                if s=="OK":
                    continue
                else:
                    break
                # time.sleep(1)
                print(data)
            else:
                time.sleep(2)
                print("队列中没有数据")
    except:
        client.send(b'Q')
